package top.v5it.japi.plus.spring.boot.autoconfigure;

import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.HibernateValidator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import top.v5it.japi.plus.spring.boot.autoconfigure.properties.JapiProperties;

import javax.validation.Validation;
import javax.validation.Validator;


/**
 * 启用Validator验证器
 *
 * @author zhanpu
 * @date 2021/3/13
 */
@Slf4j
@ConditionalOnProperty(name = "spring.japi.validator.enabled", havingValue = "true")
public class ValidatorConfiguration {

    /**
     * 在当前应用程序中注入Validator验证器
     *
     * @param japiProperties {@link JapiProperties}
     * @return {@link Validator}
     */
    @Bean
    public Validator validator(JapiProperties japiProperties) {
        log.info(">>>>>>>>>>[Init Validator]<<<<<<<<<<");

        Validation.buildDefaultValidatorFactory();
        return Validation.byProvider(HibernateValidator.class)
                .configure()
                .failFast(japiProperties.isFailFast())
                .buildValidatorFactory()
                .getValidator();
    }
}
